# coding: utf-8

import os
import web
import uuid
import Image
import datetime
from web import form

import db
import skate
from config import DIR
from libs import *

__all__ = ['index', 'login', 'setting', 'logout', 'signup']

login_form = form.Form(
        form.Textbox('email', vemail, description='你的邮件'),
        form.Password('password', form.notnull, description='你的密码'),
        validators = [
            form.Validator('Email或密码错误', lambda i:
                len(skate.db.select('user', where='email="%s" and password="%s"' % (
                    i.email, i.password))) > 0)
            ]
        )

def check_logined():
    if skate.session.id != -1:
        skate.session.show_msg = True
        skate.session.msg = "你已登录"
        return web.seeother("news")

class index:

    def __get_acts(self):
        acts = db.get_limit_update_acts(15)
        return recover_datetime(acts)

    def GET(self):

        check_logined()
        acts = self.__get_acts()
        f = login_form()

        return skate.render.index(acts, f)


class login:

    def GET(self):

        check_logined()
        f = login_form()
        return skate.render.login(f)

    def POST(self):

        f = login_form()
        if not f.validates():
            skate.session.show_msg = True
            skate.session.msg = "登录失败"
            return skate.render.login(f)
        else:
            user = skate.db.select('user', where='email="%s"' % f['email'].value)[0]
            skate.session.user = user['nickname']
            skate.session.id = int(user['id'])
            if user['super'] == 1:
                skate.session.super = True

            db.update_user_lastactive(
                    datetime.datetime.now(), web.ctx.ip, skate.session.id)

            skate.session.show_msg = True
            skate.session.msg = "登录成功"
            web.seeother("news")

class logout:

    def GET(self):
        skate.session.user = 'anonymous'
        skate.session.id = -1
        skate.session.super = False
        skate.session.show_msg = False
        skate.session.msg = ''

        web.seeother("index")


class signup:

    signup_form = form.Form(
            form.Textbox('email', vemail, description='你的Email地址'),
            form.Password('password', vpass, description='你的密码'),
            form.Textbox('nickname', form.notnull, description='你的昵称'),
            form.Checkbox('agree', value="on", checked=False, description="是否同意协议"),
            validators = [
                form.Validator("该帐号已存在", lambda i:
                    len(skate.db.select('user', where='email="%s"' % i.email)) == 0),
                ]
            )

    def GET(self):
        f = self.signup_form()
        return skate.render.signup(f)

    def POST(self):
        f = self.signup_form()
        if not f.validates():
            skate.session.show_msg = True
            skate.session.msg = "注册失败"
            return skate.render.signup(f)

        else:
            uid = db.new_user(
                    f['email'].value, f['password'].value,
                    f['nickname'].value, 
                    web.ctx.ip, datetime.datetime.now(),
                    web.ctx.ip, datetime.datetime.now()
                    )

            seqid = db.new_news(NEWS['newuser'], uid=uid)

            skate.session.show_msg = True
            skate.session.msg = "注册成功"
            return web.seeother("login")
